<?php
	
	session_start();
	/**
	* @package admin
	* @desc This php file is used to run to function to either delete or edit a park on the park detail page.
	*/
	// Direct calling check
	if ($_SESSION["role"] != "admin")
	{
		header('Location: ../index.php');
		exit();
	}
	if(!isset($_GET['action']) || !isset($_GET['id']))		
	{
		header('Location: findpark.php');
		exit();
	}
		
	/**
	* @desc This include file will load the directory settings for the Smarty Templates
	*/
	require '../includes/smarty.inc';
	$smarty->assign('title', "Manage Park");
	$smarty->assign('user_id', $_SESSION["user_id"]);	
	$smarty->assign('last', $_SESSION["lastviewed"]);
	$smarty->assign('firstname', $_SESSION["firstname"]);
	$smarty->assign('role', $_SESSION["role"]);
	$smarty->assign('top5', $_SESSION["top5"]);	
	$smarty->assign('pathdepth', "../");
	$smarty->display('header.tpl');
	
		
	// Process actions and input
	$action = $_GET["action"];
	$park_id = $_GET["id"];
	
	/**
	* @desc This include file connects to the database.
	*/
	require '../includes/connect.inc';
	
	//prepare query
	$query = "SELECT * FROM parks WHERE park_id = $park_id";
	
	//run query
	$result = mysql_query($query, $connection) or die("Could not load park info");
	
	$row = mysql_fetch_array($result);
	
	$park_id = $row["park_id"];
	$park_suburb = $row["suburb_id"];
	$park_type = $row["type"];
	$park_name = $row["name"];
	$park_address = $row["address"];
	
	//get suburbs
	$suburb_name_query = "SELECT name FROM suburbs WHERE suburb_id = $park_suburb";
	$query = "SELECT suburb_id, name FROM suburbs WHERE suburb_id != $park_suburb";
	
	//run query for suburbs
	$result_suburb_name = mysql_query($suburb_name_query, $connection) or die("Could not load park suburb info");
	$result = mysql_query($query, $connection) or die("Could not load park info");
	
	$suburb_name = mysql_fetch_array($result_suburb_name);
	$park_suburb_name = $suburb_name["name"];


	
	//check if pic exists
	$query = "SELECT id FROM pictures WHERE id = $park_id";
	
	$picexists = mysql_query($query, $connection);
	if(mysql_num_rows($picexists) > 0)
		$src = "../showpic.php?id=$park_id";
	else
		$src = "../images/noimage.png";
	
	$smarty->assign('src', $src);
	
	$smarty->assign('id', $park_id);
	$smarty->assign('name', $park_name);
	$smarty->assign('type', $park_type);
	$smarty->assign('address', $park_address);
	
	switch ($action)
	{
		case "delete":
			$smarty->assign('suburb', $park_suburb_name);
			$smarty->display('admin/deletepark.tpl');			
			break;
			
		case "edit":	
			
			$smarty->assign('suburb', $park_suburb);
			
			//get park suburb
			$query = "SELECT suburb_id, name, postcode FROM suburbs";
			if (!$result = mysql_query ($query, $connection))
				$smarty->assign('failPC', 'Fail to load postcodes');
			else
			{		
				$arOptionsPC = array();
				//Iterate through results, extracting park types		
				while($row = mysql_fetch_array($result))
					$arOptionsPC[] = array('id' => $row[0], 'suburb' => $row[1], 'postcode' => $row[2]);
				$smarty->assign('optionsPC', $arOptionsPC);
			}
		
			//get different equipment for this park
			$query = "SELECT * FROM equipment";
			
			$result = mysql_query($query, $connection) or die("Could not load park equipment");
			
			$query2 = "SELECT equip_id, qty FROM parkequipment WHERE park_id = $park_id";
			$result2 = mysql_query($query2, $connection);

			$arEquip = array();
			
			if ($result2)
			{
				// Iterate equipment
				while ($row2 = mysql_fetch_array($result2))
					$arEquip[$row2["equip_id"]] = $row2["qty"];
				$smarty->assign('oldEq', $arEquip);
			}
			else
			{
				$oldEqFail = "Could not load park equipment details";
				$smarty->assign('oldEqFail', $oldEqFail);
			}			

			$equip = array();
			$numrows = mysql_num_rows($result);
			
			//while ($row = mysql_fetch_array($result))
			for($i = 1; $i <= $numrows; $i++)
			{
				$row = mysql_fetch_array($result);
				if(isset($_SESSION["form"][$i]))
				{
					$checked = true;
				}
				else
				{
					$checked = false;
				}		
				$equip[] = array('id' => $row["equip_id"], 'name' => $row["name"], 'checked' => $checked);
			}
			
			$smarty->assign('equip', $equip);
			
			$_SESSION["equip"] = $arEquip;
			
			if(isset($_SESSION["errors"]["name"]))
				$smarty->assign('errorsname', $_SESSION["errors"]["name"]);
	
			if(isset($_SESSION["errors"]["address"]))
				$smarty->assign('errorsaddress', $_SESSION["errors"]["address"]);
			
			if(isset($_SESSION["errors"]["type"]))
				$smarty->assign('errorstype', $_SESSION["errors"]["type"]);
			
			if(isset($_SESSION["errors"]["pic"]))
				$smarty->assign('errorspic', $_SESSION["errors"]["pic"]);
			
			$errors = array();
			$form = array();
			for($i = 0; $i < $numrows; $i++)
			{
				if(isset($_SESSION["errors"][$i+1]))
					$errors[$i] = $_SESSION["errors"][$i+1];
				else
					$form[$i] = $_SESSION["form"][$i+1];
			}
			
			$smarty->assign('errors', $errors);
			$smarty->assign('form', $form);
				
			$smarty->display('admin/editpark.tpl');		
			break;
	}
	unset($_SESSION["errors"]);	
	unset($_SESSION["form"]);
	
	
	$smarty->assign('pathdepth', "../");
	$smarty->display('footer.tpl');
	
?>